import java.applet.Applet;
import java.awt.Component;
import java.awt.Container;
import java.awt.PopupMenu;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.Desktop;

/**
 * 
 */

/**
 * @author terratack
 *
 */
public class Watek extends Thread {
	private Socket client = null;
    private BufferedReader in = null;
    private PrintWriter out = null;
	public Watek(Socket client){
        try {
        	this.client = client;
			in = new BufferedReader(new InputStreamReader(client.getInputStream()));
			this.out = new PrintWriter(client.getOutputStream(), true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void run(){
        DataOutputStream out=null;
        File h=new File("C:/Users/terratack/Desktop/Workspace/Poker/Applet.html");
        File j=new File("C:/Users/terratack/Desktop/Workspace/Poker/PokerApplet.jar");
        DataInputStream htmlFile;
        DataInputStream jarFile;
        BufferedReader br=null;
        try {
            br=new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new DataOutputStream(client.getOutputStream());
            htmlFile = new DataInputStream(new FileInputStream(h));
            jarFile=new DataInputStream(new FileInputStream(j));
            int hlen = (int) h.length();
            int jlen=(int) j.length();
            byte[] hbuf = new byte[hlen];
            byte[] jbuf = new byte[jlen];
            htmlFile.readFully(hbuf);
            jarFile.readFully(jbuf);
            htmlFile.close();
            jarFile.close();
            String request=br.readLine();
            System.out.println(request);
            if(request.startsWith("GET / HTTP/1.1")){
                out.writeBytes("HTTP/1.1 200 OK\r\n");
                out.writeBytes("Content-Length: " + h.length() + "\r\n");
                out.writeBytes("Content-Type: text/html\r\n\r\n");
                out.write(hbuf);
            }
            if(request.startsWith("GET /PokerApplet.jar HTTP/1.1")){
                out.writeBytes("HTTP/1.1 200 OK\r\n");
                out.writeBytes("Content-Length: " + j.length() + "\r\n");
                out.writeBytes("Content-Type: application/java-archive\r\n\r\n");
                out.write(jbuf);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch(NullPointerException e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
			String line = in.readLine();
			if(line.compareTo("start") == 0){
				System.out.println("polaczylem sie z watkiem");
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        try {
            out.close();
            client.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

	}

}
